package com.hansen.library.ui.widget.refresh;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import com.hansen.library.R;
/**
 * Created by han on 2018/11/1 0001.
 */

public class MSwipeRefreshLayout extends SwipeRefreshLayout {

    private int touchSlop;
    private float prevX;
    private boolean declined;

    public MSwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public MSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        setColorSchemeResources(R.color.color_main, R.color.color_main_deep);

        touchSlop = ViewConfiguration.get( context ).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent( MotionEvent event ) {
        switch( event.getAction() ){
            case MotionEvent.ACTION_DOWN:
                prevX = MotionEvent.obtain( event ).getX();
                declined = false; // New action
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = event.getX();
                float xDiff = Math.abs( eventX - prevX );
                if( declined || xDiff > touchSlop ){
                    declined = true; // Memorize
                    return false;
                }
                break;
        }

        return super.onInterceptTouchEvent( event );
    }
}
